package com.zhiyou.news.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.model.News;
import com.zhiyou.news.service.NewsService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("news/")
public class NewsController {

	@Autowired
	NewsService newsService;

	// 查询所有公告
	@GetMapping(value = "list")
	public JsonResult show(HttpServletRequest req, HttpServletResponse resp) {

		return new JsonResult(newsService.select(), "查询成功");

	}

	// 增加公告
	@PostMapping("add")
	public JsonResult add(News news, HttpServletRequest req, HttpServletResponse resp) {
		System.out.println(news);
		newsService.add(news);
		return new JsonResult("增加成功");
	}

	// 根据id删除公告
	@DeleteMapping("delete/{id}")
	public JsonResult delete(@PathVariable("id") int id, HttpServletRequest req, HttpServletResponse resp) {
		newsService.delete(id);
		return new JsonResult("删除成功");

	}

	// 修改公告
	@PutMapping("update")
	public JsonResult update(News news, HttpServletRequest req, HttpServletResponse resp) {
		newsService.update(news);
		return new JsonResult("修改成功");
	}

	// 阅读次数加一
	@PutMapping("updatenum")
	public JsonResult updatenum(int id, HttpServletRequest req, HttpServletResponse resp) {
		System.out.println();
		newsService.updatenum(id);

		return new JsonResult("修改num成功");

	}
}